﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using yq.ControllerBase;
using System.Web.Mvc;
using yq.WebModel;
using yq.WebModel.Sys;

namespace yq.Controller.Sys
{
    /// <summary>
    /// 团队管理
    /// </summary>
    public class TeamController : BaseController
    {
        /// <summary>
        /// 查询团队列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            yq.Search.Team.TeamListSeek search = new Search.Team.TeamListSeek();

            return View(search);
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [ErrorFilter("列表获取失败", ErrorFilterType.Text)]
        public ActionResult List(Search.Team.TeamListSeek search)
        {
            TryUpdateModel(search);
            TryUpdateModel(search.Pager);

            var list = yq.GetService.Create<yq.IService.ITeam>().SelectTeamPager(search);
            ListPage<yq.WebModel.Sys.Team> ListPage = new ListPage<yq.WebModel.Sys.Team>();
            ListPage.SetPageList(list);
            ListPage.AddRoleLinkButton("查看", "ViewTeam", "Team");
            ListPage.AddRoleLinkButton("编辑", "EidtTeam", "Team");
            ListPage.AddRoleLinkButton("成员", "TeamUserList", "Team");
            ListPage.AddRoleLinkButton("添加团队", "AddTeam", "Team");

            return PartialView(ListPage);
        }

        /// <summary>
        /// 查询团队
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("teamId")]
        public ActionResult ViewTeam()
        {
            var info = this.GetModel<yq.Model.Logic.TeamList>();

            yq.WebModel.Sys.Team model = new WebModel.Sys.Team();
            model.SetValue(info);

            return PartialView(model);
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("teamId")]
        public ActionResult EidtTeam()
        {
            var info = this.GetModel<yq.Model.Logic.TeamList>();

            return View(info);
        }

        /// <summary>
        /// 执行编辑
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("TeamId")]
        [ErrorFilter("编辑失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoEidtTeam()
        {
            yq.Model.Logic.TeamList team = this.GetModel<yq.Model.Logic.TeamList>();
            TryUpdateModel(team);
            var rv = yq.GetService.Create<yq.IService.ITeam>().EidtTeam(team);

            return Json(rv);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public ActionResult AddTeam()
        {
            yq.Model.Logic.TeamList team = new Model.Logic.TeamList();

            return View(team);
        }

        /// <summary>
        /// 执行添加
        /// </summary>
        /// <returns></returns>
        [ErrorFilter("添加失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoAddTeam()
        {
            yq.Model.Logic.TeamList team = new Model.Logic.TeamList();
            TryUpdateModel(team);
            var rv = yq.GetService.Create<yq.IService.ITeam>().AddTeam(team);

            return Json(rv);
        }

        /// <summary>
        /// 团队成员列表
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("TeamId")]
        public ActionResult TeamUserList()
        {
            var info = this.GetModel<yq.Model.Logic.TeamList>();
            var list = yq.GetService.Create<yq.IService.ITeam>().SelectTeamUserList(info.TeamId);
            TeamUserListPage listPage = new TeamUserListPage();
            listPage.TeamId = info.TeamId;

            listPage.SetList(list);
            listPage.AddRoleLinkButton("添加成员", "UserSelect", "Team");
            listPage.AddRoleLinkButton("移除", "DelTeamUser", "Team");
            listPage.AddRoleLinkButton("设置管理员", "DoTeamUserAdmin", "Team");
            listPage.AddRoleLinkButton("返回列表", "Index", "Team");

            return View(listPage);
        }

        /// <summary>
        /// 查询用户
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("TeamId")]
        public ActionResult UserSelect()
        {
            yq.WebModel.Sys.UserSelect search = new yq.WebModel.Sys.UserSelect();
            search.TeamId = this.GetModel<yq.Model.Logic.TeamList>().TeamId;

            return PartialView(search);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public ActionResult DoUserSelect(yq.WebModel.Sys.UserSelect search)
        {
            TryUpdateModel(search);
            TryUpdateModel(search.Pager);

            var list = yq.GetService.Create<yq.IService.IUser>().SelectAccountPagerList(search as yq.Search.User.AccountSeek);
            ListPage<yq.WebModel.Sys.TeamUser> ListPage = new ListPage<yq.WebModel.Sys.TeamUser>();
           
            ListPage.SetPageList(list);

            //写入特殊值
            ListPage.PageList.ForEach(obj =>
            {
                obj.TeamId = search.TeamId;
                obj.IsTeam = yq.GetService.Create<yq.IService.ITeam>().IsTeam(obj.UserId, obj.TeamId) ? yq.Enums.BooleanType.Yes : Enums.BooleanType.No;
            }
            );

            ListPage.AddRoleLinkButton("选择", "DoAddTeamUser", "Team");

            return PartialView(ListPage);
        }

        /// <summary>
        /// 添加成员
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isAdmin"></param>
        /// <returns></returns>
        [VerifyFilter("TeamId")]
        [ErrorFilter("添加成员失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoAddTeamUser(string userid)
        {
            yq.Model.Logic.TeamUser user = new Model.Logic.TeamUser();
            var model = this.GetModel<yq.Model.Logic.TeamList>();
            user.TeamId = model.TeamId;
            user.UserId = userid;

            //添加到成员表
            var rv = yq.GetService.Create<yq.IService.ITeam>().AddTeamUser(user);

            return Json(rv);
        }

        /// <summary>
        /// 改变管理员
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("rowsId")]
        [ErrorFilter("添加到管理员失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoTeamUserAdmin()
        {
            var user = this.GetModel<yq.Model.Logic.TeamUser>();
            var rv = yq.GetService.Create<yq.IService.ITeam>().AddTeamUserAdmin(user);

            return Json(rv);
        }

        /// <summary>
        /// 移除成员
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("rowsId")]
        public ActionResult DelTeamUser()
        {
            var user = this.GetModel<yq.Model.Logic.TeamUser>();
            var rv = yq.GetService.Create<yq.IService.ITeam>().DelTeamUser(user.RowsId);

            return Json(rv);
        }
    }
}
